<html>
<head><meta charset="utf-8"><title>are optimizations allowed to change program results? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html">are optimizations allowed to change program results?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="164443902"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164443902" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164443902">(Apr 29 2019 at 11:31)</a>:</h4>
<p>In some Rust targets, like <code>i586</code>, optimizations currently change the program results, e.g., when FP arithmetic is involved</p>



<a name="164443972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164443972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164443972">(Apr 29 2019 at 11:32)</a>:</h4>
<p>For example, when the x87 FPU is used, and the precision of the FPU registers is not appropriately set, which is expensive, at different optimization levels (e.g. <code>-C opt-level=0</code> vs <code>-C opt-level=3</code>) sample programs produce different results.</p>



<a name="164443982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164443982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164443982">(Apr 29 2019 at 11:32)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/123" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/123">https://github.com/rust-lang/unsafe-code-guidelines/issues/123</a></p>



<a name="164444010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444010">(Apr 29 2019 at 11:33)</a>:</h4>
<p>The very definition of a correct optimization is that it does not change what the program does. Or rather, more carefully stated: every behavior of the optimized program must have been a possible behavior of the source program.</p>



<a name="164444011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444011">(Apr 29 2019 at 11:33)</a>:</h4>
<p>So the way I see it, either whatever happens on x87 is unsound, or the semantics are not what we think they are.</p>



<a name="164444012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444012">(Apr 29 2019 at 11:33)</a>:</h4>
<p>Debug builds and release builds behaving differently is not an issue <em>per se</em>, it just means that the program is non-deterministic and the compiler chose different executions.</p>



<a name="164444015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444015">(Apr 29 2019 at 11:33)</a>:</h4>
<p>I think it might make sense to, as part of the glossary of the UCGs, define what an optimization is, because we already guarantee some (e.g. niche Optimizations)</p>



<a name="164444023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444023">(Apr 29 2019 at 11:33)</a>:</h4>
<p>niche optimizations isnt really an optimization in this sense, yeah</p>



<a name="164444068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444068">(Apr 29 2019 at 11:34)</a>:</h4>
<p>it's more part of the data layout stuff that is partially unspecified</p>



<a name="164444070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444070">(Apr 29 2019 at 11:34)</a>:</h4>
<p>and maybe answer the fundamental question of whether rust optimizations are allowed to change rust program semantics or not (without going into FP), and maybe RFCing that</p>



<a name="164444077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444077">(Apr 29 2019 at 11:34)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> indeed, niche optimizations are a bad example</p>



<a name="164444082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444082">(Apr 29 2019 at 11:34)</a>:</h4>
<blockquote>
<p>and maybe answer the fundamental question of whether rust optimizations are allowed to change rust program semantics or not (without going into FP), and maybe RFCing that</p>
</blockquote>
<p>uh. of course they are not. that's like asking if the rust compiler is allowed to erase your hard disk. we dont need an RFC for that, there is decades of research on what is a compiler.^^</p>



<a name="164444111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444111">(Apr 29 2019 at 11:35)</a>:</h4>
<p>it might be worth repeating that here to have it documented, but having an RFC on this to me feels like having an RFC on the value of pi.^^</p>



<a name="164444797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444797">(Apr 29 2019 at 11:47)</a>:</h4>
<p>indeed | EDIT: this was part of a private thread, so what follows does not make much sense here - the idea being explored here was how miri could implement fp non-determinism, e.g, by storing multiple values per float (e.g. an f64 and an f80 one)</p>



<a name="164444869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444869">(Apr 29 2019 at 11:48)</a>:</h4>
<p>so per FP stack you don't only need two values, f64 and f80, you need all possible values</p>



<a name="164444889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444889">(Apr 29 2019 at 11:49)</a>:</h4>
<p>"FP stack"?</p>



<a name="164444903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444903">(Apr 29 2019 at 11:49)</a>:</h4>
<p>e.g. if you want to diagnose <code>if x &gt; y { unsafe { foo() }</code> and see if that can be called, where x and y have arrived from a chain of N and M FP arithmetic operations</p>



<a name="164444922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444922">(Apr 29 2019 at 11:49)</a>:</h4>
<p>each of which individual operation could have been performed with a different precision, you have many x and many y possible values</p>



<a name="164444983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164444983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164444983">(Apr 29 2019 at 11:50)</a>:</h4>
<p>and are probably interested in "will this branch always or never be taken"</p>



<a name="164445014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164445014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164445014">(Apr 29 2019 at 11:51)</a>:</h4>
<p>that is, if you want to avoid executing the program an infinite amount of times, you'd have to track all FP values that are getting accumulated</p>



<a name="164445087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164445087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164445087">(Apr 29 2019 at 11:52)</a>:</h4>
<p>sure, the usual problem with non-determinism is that it can be infeasible to explore all possible executions</p>



<a name="164446963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164446963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164446963">(Apr 29 2019 at 12:24)</a>:</h4>
<p>so I asked on #llvm in IRC and it seems that fixing this for i586 would be too expensive, and even then might not work properly</p>



<a name="164447018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164447018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164447018">(Apr 29 2019 at 12:25)</a>:</h4>
<p>the best bet would be to make all loads and stores of f32 and f64 in i586 volatile, instead of playing with rounding modes</p>



<a name="164447565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164447565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164447565">(Apr 29 2019 at 12:33)</a>:</h4>
<p>that would not produce the same results as SSE, but that's actually something we could implement in miri</p>



<a name="164484215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164484215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164484215">(Apr 29 2019 at 20:07)</a>:</h4>
<p>Yes, it's not fundamentally difficult to force some arbitrary deterministic behavior out of x87 if you accept a big performance hit. But that doesn't solve the actual underlying question for UCG: what degree of non-determinism in floating point is possible (and hence, how much can results vary between different executions). Some amount is clearly needed to account for platform differences and compiler optimizations (see above, also: NaN bit patterns) but presumably there should be some limits even in the worst case?</p>



<a name="164491751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164491751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tom Phinney <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164491751">(Apr 29 2019 at 21:46)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span>  FP arithmetic is imprecise: optimizations can and often do change program results. You don't have to conditionally-invoke <code>unsafe</code> behavior to show this; all you need do is apply the associative and distributive laws of arithmetic to reorder the terms and/or factors in a computation, which is something that LLVM-like optimizers do. Often such reordering will lead to slightly different FP numeric results, which when compared with an "expected" result can lead to taking different branches in a program.</p>



<a name="164491824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164491824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164491824">(Apr 29 2019 at 21:47)</a>:</h4>
<p><span class="user-mention" data-user-id="132894">@Tom Phinney</span> we dont do those optimizations, and llvm doesn’t do them by default either</p>



<a name="164491891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164491891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164491891">(Apr 29 2019 at 21:48)</a>:</h4>
<p>One needs to opt-into -ffast-math in llvm and clang because of that</p>



<a name="164491909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164491909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164491909">(Apr 29 2019 at 21:48)</a>:</h4>
<p>So AFAICT the x87 situation is an LLVM bug</p>



<a name="164491961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164491961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164491961">(Apr 29 2019 at 21:49)</a>:</h4>
<p>On IRC some devs mentioned they would accept patches for that target, even if they make things much slower for the target by default</p>



<a name="164492062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164492062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164492062">(Apr 29 2019 at 21:51)</a>:</h4>
<p>I don’t know whether that would really end up happening, but optimizations should not change your program results</p>



<a name="164492089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164492089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164492089">(Apr 29 2019 at 21:51)</a>:</h4>
<p>At least not without explicitly opting into unsound optimizations</p>



<a name="164494923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164494923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164494923">(Apr 29 2019 at 22:39)</a>:</h4>
<blockquote>
<p>So AFAICT the x87 situation is an LLVM bug</p>
</blockquote>
<p>I think this is quite arguable. The behavior of LLVM's codegen on x87 is closer to what FLT_EVAL_METHOD in standard C blesses as an implementation choice (only that the extra precision is not stripped at source-level assignments and casts but at ill-defined other points) than to the decidedly more shaky -ffast-math. You could argue LLVM IR should have stricter semantics by default and make the current behavior opt-in but AFAICT it deliberately doesn't (it's important for many targets, including very modern ones, to have some leeway in IEEE conformance). An option for more determinism would be great, but I think we can only seriously contemplate this in 2019 because nobody really cares about non-SSE2 hardware any more, and for the very same reason it'll be hard to get people to care enough to even classify this as a bug that needs fixing instead of sweeping it under the rug as being covered by the lack of guarantees about IEEE conformance.</p>



<a name="164514396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164514396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164514396">(Apr 30 2019 at 06:13)</a>:</h4>
<p>Which other modern targets have this particular behavior?</p>



<a name="164516316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164516316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164516316">(Apr 30 2019 at 06:59)</a>:</h4>
<p>Most / all LLVM FP intrinsics do mention IEEE conformance in the LangRef (and the standard is exact for many operations, e.g., +-*...), and there are fast-math flags in place to enable this kind of behavior already AFAICT</p>



<a name="164516897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164516897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164516897">(Apr 30 2019 at 07:11)</a>:</h4>
<p>Various embedded platforms and GPUs have settings like "flush denormals to zero". Even when those are optional settings, they are commonly enabled and compilers want to be able to Just Work in their presence (unlike e.g. access to floating point exceptions and changes to rounding mode, which need special accomodation). And then there's the fact that some operations may be lowered to libcalls that are, in practice, not as accurate as IEEE mandates, but LLVM still wants to be able to generate code for those targets.</p>



<a name="164516984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are%20optimizations%20allowed%20to%20change%20program%20results%3F/near/164516984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/are.20optimizations.20allowed.20to.20change.20program.20results.3F.html#164516984">(Apr 30 2019 at 07:13)</a>:</h4>
<p>I just took another look at LangRef and indeed some intrinsics make reference to the corresponding libm function, but fadd/fsub/fmul/fdiv notably don't refer to IEE 754!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>